package net.jxta.myjxta.plugins.groupchat;

import net.jxta.myjxta.MyJXTA;
import net.jxta.myjxta.dialog.Dialog;
import net.jxta.myjxta.dialog.DialogManager;
import net.jxta.myjxta.dialog.DialogNamer;
import net.jxta.myjxta.plugin.IPluginNotificationHandler;
import net.jxta.myjxta.plugin.PluginBase;
import net.jxta.myjxta.plugin.PluginContainer;
import net.jxta.myjxta.util.Group;
import net.jxta.myjxta.util.Resources;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.util.JxtaBiDiPipe;

import java.util.ResourceBundle;

/**
 * This Plugin provides/activates the propagation pipe based groupchat
 * It does:
 * - registers the DialogNamer (dont understand the full concept behind the namer)
 * - registers a dialog provider (the provider creates the needed propagatoin pipe)
 */
public class GroupChatPlugin extends PluginBase implements IPluginNotificationHandler, DialogManager.IDialogProvider {

    private static final ResourceBundle STRINGS = Resources.getStrings();

    public GroupChatPlugin() {
        LOG.log(java.util.logging.Level.FINE, "GroupChat Constructor called");
        setName(null); //this is an invisible plugin 
        //(not visible in the plugins menu)

    }

    public IPluginNotificationHandler getPluginNotificationHander() {
        return this;
    }

    public void init(PluginContainer c) {
        super.init(c);

        //nano: maybe we should consolidate DialogNamer and DialogProvider?
        //register the dialog namer (this will provide the name for the pipe-advertisments)
        Dialog.registerDialogNamer(ManyToManyDialog.class, new DialogNamer() {
            public String getDialogName(String s) {
                return ManyToManyDialog.DIALOG_NAME +
                        Dialog.IMFREE_DELIMITER + s;
            }

        });

        //this tells the DialogManager that we are able to instantiate a ManyToManyDialog
        DialogManager.registerDialogProvider(ManyToManyDialog.class, this);

        start(); //autostart
    }


    public void groupJoined(final Group p_group) {
        //on any group join we register our dialog name at the
        //correct dialogmanager
        if (!p_group.isVisible()) //no listeners for npg and other non-visible groups
            return;


        m_container.getMyJxta().setStatus(STRINGS.getString("status.dialog.many.register"));

        String dialogName1;
        PeerGroup pg = p_group.getPeerGroup();
        dialogName1 = Dialog.getDialogNamer(ManyToManyDialog.class).getDialogName(pg.getPeerName());

        final MyJXTA myJxta = m_container.getMyJxta();
        myJxta.setStatus(STRINGS.getString("status.dialog.listener.add") +
                ": " + dialogName1);

        final DialogManager propagatePipeDialogManager = DialogManager.getInstance(p_group, dialogName1, PipeService.PropagateType);
        //this is the pipeAdvertisment of the propagation pipe used
        //for the groupchat
        PipeAdvertisement pipeAdvertisment = propagatePipeDialogManager.getPipeAdv(pg);

        //instantiate the ManyToManyDialog (this will open the propagation pipe)
        final Dialog dialog = DialogManager.getDialog(ManyToManyDialog.class, p_group, pipeAdvertisment, myJxta);
        myJxta.addDialog(dialog);
    }

    public void groupResigned(Group p_group) {

        if (!p_group.isVisible())
            return;
        final String peerName = p_group.getPeerGroup().getPeerName();
        final MyJXTA myJxta = m_container.getMyJxta();

        String pn = Dialog.getDialogNamer(ManyToManyDialog.class).
                getDialogName(peerName);

        myJxta.setStatus(STRINGS.getString("status.dialog.listener.remove") +
                ": " + pn);

        DialogManager.getInstance(p_group, pn, PipeService.PropagateType).
                clearPipeListeners(p_group.getPeerGroup());


    }

    public void groupStateChanged(Group p_group) {
        //nothing in here - this method is called if we get connected/disconnected from a joined group
        //we could possibly activate/deactivate the send button here
        //but this is currently done inside ManyToManyDialog (may be we moved it towards this location later)
    }


    public Dialog createDialog(Group p_g, PipeAdvertisement p_pa, MyJXTA p_myjxta) {
        return new ManyToManyDialog(p_g, p_pa, p_myjxta);
    }

    public Dialog createDialog(Group p_g, JxtaBiDiPipe p_pipe, MyJXTA p_myjxta) {
        return null; //this plugin does not respond to incomming bidi-pipes
    }
}
